package db;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

public class sysuser extends Model<sysuser>{
	public static final sysuser dao = new sysuser();
	
	//增加角色的记录
	private int roles;

	//根据用户设置角色权限
	public void initRoles() {
		int irole = 0;
		String userId = this.getStr("id");
		List<Record> list = Db.find("select userid as userid,SUM(sys_role.ival) as iroles from sys_userrole left join (select id,ival from sys_role) sys_role on sys_role.id = sys_userrole.roleid where userid=? group by userid",userId);
		
		//需要增加容错，没有记录返回0
		if(list.size()>0) {
			irole = list.get(0).getInt("iroles");
		}
		this.setRoles(irole);
	}

	public void setRoles(int irole) {
		put("roles",irole);
	}
	
	public int getRoles() {
		return this.get("roles");
	}
}
